

from Tkinter import *
from PIL import Image, ImageTk
import urllib2
import tempfile




def ask_captcha(url):
	
	file = open(tempfile.mkstemp()[1], "wb")
	#print file.name
		
	file.seek(0)
	for i in urllib2.urlopen(url):
		file.write(i)
	file.flush()
		
	root = Tk()
	root.protocol("WM_DELETE_WINDOW", root.destroy)
	root.title('evlum')
	
	Label(root, text="Type in the captcha...").pack()
	
	image = Image.open(file.name)
	photo = ImageTk.PhotoImage(image)
	#photo = PhotoImage(file=file.name)
		
	label = Label(root, image=photo)
	label.image = photo # keep a reference!
	label.pack()
		
	entr = Entry(root)
	entr.focus_set()
	entr.bind('<Return>', lambda evt: btn.invoke())
	entr.pack()
		
	def _on_ok():
		result[0] = entr.get()
		#print 'destroying'
		root.destroy()
	btn = Button(root, text="ok", command=_on_ok)
	btn.pack(fill="x")
		
	result = {0: None}
	try:
		while root.winfo_exists():
			root.update()
	except TclError:
		pass
	
	file.close()
	return result[0]







